﻿using System;
using System.Collections;
using System.Text;
using System.Text.RegularExpressions;
using System.Web;
namespace Utility
{
    /// <summary>
    /// 字符串转换
    /// </summary>
    public class ConverString
    {
        #region 将软回车转换为Html的回车        
        /// <summary>
        /// 将软回车转换为Html的回车
        /// </summary>
        /// <param name="_S"></param>
        /// <returns></returns>
        public static string String_Br(string _S)
        {
            if (string.IsNullOrEmpty(_S)) { return ""; }
            _S = _S.Replace(" ", "&nbsp;");
            return _S.Replace("\r\n", "<br/>").Replace("\n\r","");
        }
        public static string String_BrToRn(string _S)
        {
            if (string.IsNullOrEmpty(_S)) { return ""; }
            _S = _S.Replace("&nbsp;", " ");
            return _S.Replace("<br/>", "\r\n");
        }
        #endregion

        #region 静态方法：提取字符串中的网址，自动加上链接
        /// <summary>
        /// 静态方法：提取字符串中的网址，自动加上链接
        /// </summary>
        /// <param name="_Cont"></param>
        /// <returns></returns>
        public static string String_UrlAddLink(string _Cont)
        {
            string StrRegex = @"((http://www.)|(http://)|(www.))([\w-]+\.)+[\w-]+(/[\w-./?%&=]*)?";
            Regex R = new Regex(StrRegex, RegexOptions.IgnoreCase);
            _Cont = Regex.Replace(_Cont, StrRegex, "<a href=\"http://$0\"  target=\"_blank\">$0</a>");
            _Cont = _Cont.Replace("http://http://", "http://");
            //MatchCollection M = R.Matches(_Cont);
            //for (var i = 0; i < M.Count; i++)
            //{
            //    if (ContainCount(M[i].ToString(),'.',false) > 1)
            //    {
            //        //_Cont = _Cont.Replace(M[i].ToString(), "<a href=\"http://" + M[i].ToString().Replace("http://", "") + "\" target=\"_blank\">" + M[i].ToString() + "</a> ");
            //    }
            //}
            return _Cont;
        }
        #endregion

        #region 统计一个字符在字符串中出现的次数
        public static int ContainCount(string input, char value, bool ignoreCase)
        {
            if (ignoreCase)
            {
                input = input.ToLower();
                if (Char.IsUpper(value))
                {
                    value = Char.ToLower(value);
                }
            }
            int count = 0;
            for (int i = 0; (i = input.IndexOf(value, i)) >= 0; i++)
            {
                count++;
            }
            return count;
        }
        #endregion

        #region 过滤html标签
        /// <summary>
        /// 过滤html标签
        /// </summary>
        /// <param name="contents">要过滤的字符串</param>
        /// <returns></returns>
        public static string RemoveHtml(string contents)
        {
            string strTmp = null;
            strTmp = Regex.Replace(contents, "<(.[^>]*)>", " ");
            return strTmp;

        }
        #endregion

        #region 字符串MD5的加密
        /// <summary>
        /// 字符串MD5的加密
        /// </summary>
        /// <param name="Str">字符串</param>
        /// <returns></returns>
        public static string MD5Fun(string Str)
        {
            //string EnPassWordMD5 = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(Str.ToString(), "MD5");
            string EnPassWordMD5 = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(Str, "MD5").ToLower().Substring(8, 16);
            return EnPassWordMD5;
        }
        #endregion

        #region 过滤特殊字符
        /// <summary>
        /// 过滤特殊字符
        /// </summary>
        /// <param name="inputString"></param>
        /// <returns></returns>
        public static string GuoLv(string inputString)
        {
            StringBuilder retVal = new StringBuilder();

            if ((inputString != null) && (inputString != String.Empty))
            {
                inputString = inputString.Trim();
                for (int i = 0; i < inputString.Length; i++)
                {
                    switch (inputString[i])
                    {
                        case '"':
                            retVal.Append("&quot;");
                            break;
                        case '<':
                            retVal.Append("&lt;");
                            break;
                        case '>':
                            retVal.Append("&gt;");
                            break;
                        default:
                            retVal.Append(inputString[i]);
                            break;
                    }
                }
                retVal.Replace("'", " ");
            }
            return retVal.ToString();
        }
        #endregion

        #region 生成指定长度的随机数
        /// <summary>
        /// 生成指定长度的随机数
        /// </summary>
        /// <param name="_Str">字符的范围，可为空</param>
        /// <param name="_Len">随即字符的长度</param>
        /// <returns></returns>
        public static string MakeRandom(string _Str, int _Len)
        {
            if (_Str == "") { _Str = "abcdefghijklmnopqrstuvwxyz0123456789"; }
            string Re = "";
            int i;
            Random Ran = new Random();
            for (int j = 0; j < _Len; j++)
            {
                i = Ran.Next(_Str.Length);
                Re += _Str[i];
            }
            return Re;
        }
        #endregion

        #region 表情转换
        /// <summary>
        /// 表情转换
        /// </summary>
        /// <param name="Str">要替换的内容</param>
        /// <param name="FaceTable">表情配置的哈希表</param>
        /// <returns></returns>
        public static string FaceFormat(string Str, Hashtable FaceTable)
        {
            string StrRegex = @"\[/(\w{2})\]";
            Regex Reg = new Regex(StrRegex, RegexOptions.IgnoreCase);
            MatchCollection M = Reg.Matches(Str);
            for (int i = 0; i < M.Count; i++)
            {
                Str = Str.Replace(M[i].ToString(), FaceTable[M[i].ToString()].ToString());
            }
            return Str;
        }
        #endregion

        #region 过滤设定的关键字
        /// <summary>
        /// 静态方法：将系统配置的关键字进行过滤
        /// </summary>
        /// <param name="Content"></param>
        /// <param name="KeyArr"></param>
        /// <returns></returns>
        public static string FilterKey(string Content, string[] KeyArr)
        {
            StringBuilder Str = new StringBuilder(Content);
            foreach (var K in KeyArr)
            {
                Str.Replace(K, "***");
            }
            return Str.ToString();
        }
        #endregion

        #region 获取select的数值选项
        public static string GetSelectOption(int Max, int Min,int Selected)
        {
            StringBuilder S = new StringBuilder();
            int i;
            for (i = Min; i <= Max; i++)
            {
                S.Append("<option value='" + i.ToString() + "' " +( i.Equals(Selected) ? "selected='selected'" : "" )+ ">" + i.ToString() + "</option>");
            }
            return S.ToString();
        }
        #endregion
    }
}